<?php

namespace Admin\Controller;
use Admin\Builder\AdminConfigBuilder;

class TecdemController  extends AdminController {
	public function index(){

			 $dem = M('TbDemand');
			$xname=I('xname','','text');
			//$map['xdstatus'] = array('egt', 1);
			$map['rid'] = UID;		
			if ($xname !== '') {
				$map['xname'] = array('like', '%' . (string)$xname . '%');
			}
			$sql='SELECT d.*,t.fplace,t.place FROM duijie_tb_demand d,duijie_tb_tecdem t
            WHERE  d.rid = t.id';
			$list=$dem->where($map)->query($sql); 
			/* $list=$dem->alias('a')->join('duijie_tb_tecdem b on a.rid=b.id')
			->select(); */
			int_to_string($list,array('xdstatus'=>array(-1=>'未对接',0=>'对接中',1=>'对接已结束')));              //数字转为字符串					
			
			foreach ($list as $key => $value) {
				$str=$list[$key]['jsbq'];
				$mapp['id']=array('in',$str);
				$list[$key]['tids']=M("TbLable")->where($mapp)->select();
			}
			
			foreach ($list as $key => $value) {
				$str1=$list[$key]['fplace'];
				$str2=$list[$key]['place'];
				$map1['id']=array('in',$str1);
				$map2['id']=array('in',$str2);
				$list[$key]['tids1']=M("District")->where($map1)->select();
                $list[$key]['tids2']=M("District")->where($map2)->select();
			}
			
			foreach ($list as $key => $value){
				$str3=$list[$key]['ffield'];
				$str4=$list[$key]['field'];
				$map3['ID']=array('in',$str3);
				$map4['ID']=array('in',$str4);
				$list[$key]['tids3']=M("TbTeclab")->where($map3)->select();
				$list[$key]['tids4']=M("TbTeclab")->where($map4)->select();
			}
			//dump($mapp);
			$this->assign('_list', $list); 
		   //dump($list);
		   $this->display();
	}
	
	public function edit($id=0){
		
		if(IS_POST){
			$dem = M('TbDemand');
			$rst=$dem -> create();
			if($rst){
				if($dem->save($rst)!==false){
					$this->success("需求修改成功",U('index'));
				}else {
					$this->error("修改失败！");
				}
			}else {
				$this->error($rst->getError());
			}
			
		}else{
			$info = array();
			$dem = M('TbDemand');
			
			/*  $sql='SELECT d.*,t.fplace,t.place FROM duijie_tb_demand d,duijie_tb_tecdem t
            WHERE  d.rid = t.id';
			$data=$dem->where($map['id']=$id)->query($sql); */
			//$map['duijie_tb_demand.id']=$id;
			$data=$dem->join('duijie_tb_tecdem on duijie_tb_tecdem.id=duijie_tb_demand.rid')
			->field('duijie_tb_tecdem.id tid,duijie_tb_tecdem.rName,duijie_tb_tecdem.xtelephone,duijie_tb_demand.*')
			->find();
			//->where($map)
			//->select();
			
			
			$this->assign('data',$data);
			
			$result = M('TbTeclab')->where(array('upid'=> 0))->field('id,name')->select();
			$this->assign('cate',$result);
			
			//dump($data);
			$this->display();
		}
		
	}
	
	public function add(){
		if(IS_POST){
			$dem = D("TbDemand");
			$data = $dem->create();
			$data['rid'] = UID;
			$rst = $dem->add($data);
			if($rst){
				$this->success("添加成功",U("tecach"));
			}else{
				$this->error("添加失败");
			}
		}
		
		$result = M('tb_teclab')->where(array('upid'=> 0))->field('id,name')->select();
		$this->assign('cate',$result);
		$this->display('edit');
		 
		}
	
		
		//活动中心（所有展会）
		public function allzhanhui(){
			$zhanhui = M(tb_exhinf);
		
			$sql = 'SELECT * FROM duijie_tb_exhinf;';
			$list = $zhanhui -> query($sql);
			$this -> assign('_list',$list);
		
			$this->meta_title = '展会列表';
			$this->display();
		}
		
		//我要报名
		public function enter($id=''){
		
			if(IS_POST){
		
				$exh = M(tb_exhdem);
				$update[zID] = $_POST['zid'];
				//	dump($_POST['zid']);
				$update[xID] = $_POST['xID'];
				//	dump($_POST['jID']);
				//  dump($update);
				$rst = $exh ->add($update);
				if($rst==''){
					$this->error('该需求重复报名！','',3);
				}else{
					$this->success('报名成功！','',3);
				}
		
			}else{
				$dem = M(tb_demand);
				$where['rid'] = UID;
				$data = $dem -> where($where)->select();
				$data[zid]= $id;
				//     	dump($data);
				$this->assign('data',$data);
				$this->display();
			}
		
			//	$this->display();
		}
		
		//个人中心 我参加的展会
		public function myzhanhui(){
		
			$zhanhui = M(tb_exhinf);
			$map[rid] = UID;
		
			$list = $zhanhui -> join('duijie_tb_exhdem on duijie_tb_exhdem.zID = duijie_tb_exhinf.id')
			-> join('duijie_tb_demand on duijie_tb_demand.id= duijie_tb_exhdem.xID')
			-> field('duijie_tb_exhinf.*')
			-> where($map)
			-> select();
		
			//dump($list);
			$this -> assign('_list',$list);
		
			$this->meta_title = '展会列表';
			$this->display();
		}
		
		//展会详情
		public function zdetail($id=''){
		
			$zhanhui = M(tb_exhinf);
			$data = $zhanhui -> find($id);
			$this -> assign('data',$data);
				
			$this->meta_title = '展会详情';
			$this->display();
		}
		
		//项目详情
public function xmdetail($id=''){
		
		$tecach = M(tb_tecach);
		
		$where['duijie_tb_tecach.id']= $id;
 		$data = $tecach ->join('duijie_tb_techol on duijie_tb_techol.id = duijie_tb_tecach.cID')
 		->join('duijie_tb_teccoo on duijie_tb_teccoo.cid=duijie_tb_tecach.hztype')
 		->join('duijie_tb_teccsd on duijie_tb_teccsd.sid=duijie_tb_tecach.csd')
 		->join('duijie_tb_patent on duijie_tb_patent.id = duijie_tb_tecach.patentid')
 		->join('duijie_tb_award on duijie_tb_award.ID = duijie_tb_tecach.awardid')
 		->join('duijie_tb_paper on duijie_tb_paper.id = duijie_tb_tecach.paperid')
 		->where($where) ->select();
 		
 		int_to_string($data,array('duijie'=>array(-1=>'未对接',0=>'对接中',1=>'对接已结束')));
 		
 		foreach ($data as $key => $value) {
 			$str=$data[$key]['fplace'];
 			$str2=$data[$key]['place'];
 			$map['id']=$str;
 			$map2['id']=$str2;
 			$data[$key]['tids']=M("District")->where($map)->select();
 			$data[$key]['tids2']=M("District")->where($map2)->select();
 		}
 		 
 		foreach ($data as $key => $value) {
 			$str3=$data[$key]['ffield'];
 			$str4=$data[$key]['field'];
 			$map3['ID']=$str3;
 			$map4['ID']=$str4;
 			$data[$key]['tids3']=M("TbTeclab")->where($map3)->select();
 			$data[$key]['tids4']=M("TbTeclab")->where($map4)->select();
 		}
 		
 		$this -> assign('data',$data);
 		 		
 		$this->meta_title = '项目详情';
		$this->display();
	}
		
		//参展项目
		public function zxm($id=''){
		
			$tecach = M(tb_tecach);
			$sql='SELECT * FROM duijie_tb_tecach,duijie_tb_exhpro,duijie_tb_techol
              WHERE duijie_tb_exhpro.xID = duijie_tb_tecach.id and duijie_tb_tecach.cID=duijie_tb_techol.id';
			$map[zID] = $id;
			$list = $tecach -> where($map)-> query($sql);
		
		    
			foreach ($list as $key => $value) {
				$str=$list[$key]['tID'];
				$mapp['id']=array('in',$str);
				$list[$key]['tids']=M("TbLable")->where($mapp)->select();
			}
				
			foreach ($list as $key => $value) {
				$str1=$list[$key]['fplace'];
				$str2=$list[$key]['place'];
				$map1['id']=array('in',$str1);
				$map2['id']=array('in',$str2);
				$list[$key]['tids1']=M("District")->where($map1)->select();
				$list[$key]['tids2']=M("District")->where($map2)->select();
			}
				
			foreach ($list as $key => $value){
				$str3=$list[$key]['labfid'];
				$str4=$list[$key]['labkid'];
				$map3['ID']=array('in',$str3);
				$map4['ID']=array('in',$str4);
				$list[$key]['tids3']=M("TbTeclab")->where($map3)->select();
				$list[$key]['tids4']=M("TbTeclab")->where($map4)->select();
			}
		  //dump($list);

			$this -> assign('_list',$list);
			$this->meta_title = '参展项目';
			$this->display();
		}
		
		public function auction(){
			$auction	=	M('TbAuction');
			/* $map['cID'] = UID; */
		
			$sql='SELECT * from duijie_tb_auction,duijie_tb_tecach
             WHERE duijie_tb_auction.tecID=duijie_tb_tecach.id';
			$list=$auction->query($sql);
				
			int_to_string($list,array('jstatus'=>array(-1=>'不通过' ,0=>'申请中',1=>'进行中',2=>'已结束')));
		
			$this->assign('_list', $list);
			$this->display();
		}	
		public function config(){
		
			$aUid = I('get.uid', is_login(), 'intval');
			$aTab = I('get.tab', '', 'op_t');
			$aNickname = I('post.nickname', '', 'op_t');
			$aSex = I('post.sex', 0, 'intval');
			$aEmail = I('post.email', '', 'op_t');
			$aSignature = I('post.signature', '', 'op_t');
			$aCommunity = I('post.community', 0, 'intval');
			$aDistrict = I('post.district', 0, 'intval');
			$aCity = I('post.city', 0, 'intval');
			$aProvince = I('post.province', 0, 'intval');
		
		
			if (IS_POST) {
				$this->checkNickname($aNickname);
				$this->checkSex($aSex);
				$this->checkSignature($aSignature);
		
		
				$user['pos_province'] = $aProvince;
				$user['pos_city'] = $aCity;
				$user['pos_district'] = $aDistrict;
				$user['pos_community'] = $aCommunity;
		
				$user['nickname'] = $aNickname;
				$user['sex'] = $aSex;
				$user['signature'] = $aSignature;
				$user['uid'] = get_uid();
		
				$rs_member = D('Member')->save($user);
		
				$ucuser['id'] = get_uid();
				$rs_ucmember = UCenterMember()->save($ucuser);
				clean_query_user_cache(get_uid(), array('nickname', 'sex', 'signature', 'email', 'pos_province', 'pos_city', 'pos_district', 'pos_community'));
				 
		
		
		
				//TODO tox 清空缓存
		
				S('weibo_at_who_users',null);
		
				if ($rs_member || $rs_ucmember) {
					$this->success(L('_SUCCESS_SETTINGS_').L('_PERIOD_'),U('admin/public/login'));
		
				} else {
					$this->success(L('_DATA_UNMODIFIED_').L('_PERIOD_'));
				}
		
			} else {
				//调用API获取基本信息
				//TODO tox 获取省市区数据
				$user = query_user(array('nickname', 'signature', 'email', 'mobile', 'avatar128', 'rank_link', 'sex', 'pos_province', 'pos_city', 'pos_district', 'pos_community'), $aUid);
				//显示页面
				$this->assign('user', $user);
		
				$this->accountInfo();
				$this->display();
			}
		
		}
		private function accountInfo()
		{
			$info = UCenterMember()->field('id,username,email,mobile,type')->find(is_login());
			$this->assign('accountInfo', $info);
		}
		
		public function hetong(){
			$this->display();
		}
		
		public function jiaoyi(){
			$this->display();
		}
}